//
//  ServiceViewController.m
//  mendianjieke
//
//  Created by 响当当 on 7/30/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "ServiceViewController.h"
#import "ServiceView.h"

@interface ServiceViewController ()
@property (nonatomic, strong)ServiceView *serviceView;
@property (nonatomic, strong)NSString *str;
@property (nonatomic, strong)UIView *noCountView;


@end

@implementation ServiceViewController

- (UIView *)noCountView
{
    if (!_noCountView)
       {
           _noCountView = [Utilties getNoCountViewWithFrame:CGRectMake(SCREEN_WIDTH/2-132*kUIScaleBasePlus,SCREEN_HEIGHT/2-80*kUIScaleBasePlus,264*kUIScaleBasePlus,264*kUIScaleBasePlus)
                                                      image:@"no_customer"
                                                      title:@"暂时没有服务中的客户哦~\n点击左下方实时按钮，开始接待吧"];
       }
       return _noCountView;
}


- (ServiceView *)serviceView
{
    if (!_serviceView)
    {
        _serviceView = [[ServiceView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT-TABBAR_HEIGHT)];
        WeakSelf(self)
        _serviceView.serviceLockBlock = ^{
            [weakSelf getInfo:@"1"];
            weakSelf.str = @"1";
        };
        
        _serviceView.reloadDataBlock = ^{
            [weakSelf getInfo:@"1"];
        };
    }
    return _serviceView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = LCHexColor(0xF2F2F2);
    
    [self setTitle:@"服务中"];

    [self.view addSubview:self.serviceView];
    self.str = @"0";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresToken:) name:@"RefreshToken" object:nil];

}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.viewType = 0;

    [self getInfo:@"1"];

    
}

- (void)getInfo:(NSString *)type
{
    NSDictionary *pramaters = @{
                                @"type" : type,
                                @"id" : @"",
                                @"dataTime":@""
                                };
    
    [SVProgressHUD showWithStatus:@"加载中"];

    WeakSelf(self)
    [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
        if ([weakSelf.str isEqualToString:@"1"])
        {
            DISMISS_SVP(1.0)
            [SVProgressHUD showSuccessWithStatus:@"结束服务"];
            weakSelf.str = @"0";
        }
        
        DISMISS_SVP(1.0)

        NSArray *arr = [NSArray arrayWithArray:acceptData];
              
              if (arr.count == 0)
              {
                  [weakSelf.view addSubview:weakSelf.noCountView];
              }
              else
              {
                  if (weakSelf.noCountView)
                  {
                      [weakSelf.noCountView removeFromSuperview];
                  }
              }
        [weakSelf.serviceView getInfo:IFNULL(acceptData, [NSArray array])];

    } fail:^(NSError * _Nullable error) {
        
    }];
}


- (void)reFresToken:(NSNotification *)notfi
{
    
    if (current_User.hasLogin.integerValue == 1)
    {
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RefreshToken" object:nil];
        
        NSDictionary *pramaters = @{
                                    @"type" : @"1",
                                    @"id" : @"",
                                    @"dataTime":@""
                                    };
        
        WeakSelf(self)
        [infoManager getTimeInfo:pramaters sucess:^(id  _Nonnull acceptData) {
            if ([weakSelf.str isEqualToString:@"1"])
            {
                DISMISS_SVP(1.0)
                [SVProgressHUD showSuccessWithStatus:@"结束服务"];
                weakSelf.str = @"0";
            }
            
            [weakSelf.serviceView getInfo:IFNULL(acceptData, [NSArray array])];
            
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reFresToken:) name:@"RefreshToken" object:nil];

        } fail:^(NSError * _Nullable error) {
            
        }];
    }

}

@end
